<?php

namespace App\Http\Controllers;

use App\Models\Device;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Log;
use DB;
use App\Models\DeviceDetailedList;

class CrontabController extends BaseController
{
    public static function updateWechatFriends()
    {
        if(time()>=strtotime(date('00:05:00')) && time()<=strtotime(date('00:35:00'))){
            Log::info('-------------------updateWechatFriends-----------');
            $devices = Device::with([
                'deviceDetailed'=> function ($query){
                    $query->where('detail_type',10)
                        ->where(DB::raw('date(created_at)'), date('Y-m-d'));
                }])->get();
            foreach ($devices as $row){
                if(!isset($row['deviceDetailed'][0])){//date("Y-m-d",strtotime("-1 day"))
                    $old = DeviceDetailedList::where('device_id',$row['id'])
                        ->where('detail_type',10)
                        ->where(DB::raw('date(created_at)'),date("Y-m-d",strtotime("-1 day")))
                        ->first();
                    $friends = $old ? $old->detail_count : 0;
                    if($friends > 0){
                        $query = new DeviceDetailedList();
                        $query->device_id = $row['id'];
                        $query->detail_type = 10;
                        $query->request_count = 0;
                        $query->detail_count = $friends;
                        $query->device_code = $row['device_code'];
                        $query->save();
                    }
                }
            }
        }
    }

    public static function updateWechatFriendsSpread(){
        if(time()>=strtotime(date('00:05:00')) && time()<=strtotime(date('00:35:00'))){
            Log::info('-------------------updateWechatFriendsSpread-----------');
            //前天的设备群
            $device_id_list = DeviceDetailedList::where('detail_type',10)
                ->where(DB::raw('date(created_at)'),date("Y-m-d",strtotime("-2 day")))
                ->pluck('device_id')->toArray();
            //此用户群前天和昨天的差价
            $foster = DB::select('SELECT a.device_id,a.device_code,SUM(a.detail_count) foster FROM (SELECT device_id,device_code,-detail_count detail_count FROM `device_detailed_lists` WHERE detail_type=10 AND date(created_at)=? UNION ALL SELECT device_id,device_code,detail_count detail_count FROM `device_detailed_lists` WHERE detail_type=10 AND date(created_at)=?)a GROUP BY a.device_id',[date("Y-m-d",strtotime("-2 day")),date("Y-m-d",strtotime("-1 day"))]);
            if(isset($foster[0])){
                foreach ($foster as $row){
                    if(in_array($row->device_id,$device_id_list)){
                        $exist = DeviceDetailedList::where('detail_type',15)->where('device_id',$row->device_id)->where(DB::raw('date(created_at)'),date("Y-m-d"))->first();
                        if(!$exist){
                            $query = new DeviceDetailedList();
                            $query->device_id = $row->device_id;
                            $query->detail_type = 15;
                            $query->request_count = 0;
                            $query->detail_count = $row->foster;
                            $query->device_code = $row->device_code;
                            $query->save();
                        }
                    }
                }
            }
        }
    }

    public static function createLog(){
        Log::info('createLog');//每天第一时间创建Log以确保www成为log文件拥有着
    }
}
